#!/usr/bin/env python
# encoding: utf-8

"""
@version: python3.7
@Author  : huangyi
@contact:  hy.dazui@qq.com
@Time    : 2020/1/10 0:47
@Describe: add Describe here
"""
from app.expression.abs_expression import AbsExpression
from app.expression.context import Context
from app.expression.exception.invalid_exception import InvalidException
from app.expression.nonterminal_expression import NonterminalExpression


class DivExpression(NonterminalExpression):
    """
    除法
    """

    def __init__(self, number1: AbsExpression, number2: AbsExpression):
        self.number1 = number1
        self.number2 = number2

    def interpret(self, ctx: Context):
        value2 = self.number2.interpret(ctx)
        if value2 == 0:
            raise InvalidException("被除数不能为0")
        return self.number1.interpret(ctx) / value2

    def __str__(self):
        return "{}/{}".format(self.number1, self.number2)

    def __repr__(self):
        return str(self)
